package tree;

import java.util.ArrayList;
import java.util.List;

public class NAryTreePostorderTraversal590 {
    public List<Integer> postorder(Node root) {
        List<Integer> result = new ArrayList<>();
        postorder(root, result);
        return result;
    }

    private void postorder(Node root, List<Integer> result) {
        if (root == null) {
            return;
        }
        List<Node> childrens = root.children;
        for (Node children: childrens
             ) {
            postorder(children,result);
        }
        result.add(root.val);
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
}
